package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.PsLineInfoMapper;
import com.maochang.mapper.PsPoleInfoMapper;
import com.maochang.mapper.PsSysTemConfigMapper;
import com.maochang.pojo.PsLineInfo;
import com.maochang.pojo.PsPoleInfo;
import com.maochang.pojo.PsSystemconfig;
import com.maochang.pojo.util.Result2;
import com.maochang.service.PsLineInfoService;
import com.maochang.service.PsPoleInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("PsLineInfoService")
public class PsLineInfoServiceImpl  extends ServiceImpl<PsLineInfoMapper,PsLineInfo> implements PsLineInfoService {

    @Resource
    private PsLineInfoMapper psLineInfoMapper;

    @Resource
    private PsSysTemConfigMapper psSysTemConfigMapper;

    @Resource
    private PsPoleInfoMapper psPoleInfoMapper;
    @Resource
    private PsLineInfoService psLineInfoService;


    @Override
    public Page<PsLineInfo> ChaPsLI(String name,Integer id,int curent, int page) {
        System.out.println(id+"idid");
        Page<PsLineInfo> psliPage=new Page<>(curent,page);
        QueryWrapper<PsLineInfo> wrapper= Wrappers.query();
        wrapper.like(!StringUtils.isBlank(name),"lineCode",name);
        wrapper.eq(id!=null,"runningStatus",id);
        System.out.println("qqqqname"+id);
        return psLineInfoMapper.AllPsLI(psliPage,wrapper);
    }

    /**
     * 新增改数据 会话值，杆台编号 终止编号
     * */
    @Override
    public void addPsIF(PsLineInfo psLineInfo) {
        psLineInfo.setLastUpdateTime(null);
        psLineInfo.setCreationTime(null);
        psLineInfo.setRunningStatusName("正常");
        psLineInfo.setRunningStatus(23);
        psLineInfo.setMaintenanceCompany(null);
        psLineInfo.setEndPole(null);
        psLineInfo.setStartPole(null);
        psLineInfoMapper.insert(psLineInfo);
    }

    @Override
    public boolean QxZhuAngTai(Integer id, Integer isStart) {
        PsLineInfo PsLI=new PsLineInfo();
        PsLI.setIsStart(isStart);
        QueryWrapper<PsLineInfo> wrapper= Wrappers.query();
        wrapper.eq("id",id);
        int i= psLineInfoMapper.update(PsLI,wrapper);
        boolean PsILZ=false;
        if(i>0){
            PsILZ=true;
        }
        return PsILZ;
    }

    @Override
    public boolean ShanChu(Integer id) {
        QueryWrapper<PsLineInfo> wrapper= Wrappers.query();
        wrapper.eq("id",id);
        int s=psLineInfoMapper.delete(wrapper);
        boolean fAlg=false;
        if (s>0){
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public boolean PSUpdate(PsLineInfo psLineInfo) {
        PsLineInfo psLineInfo1=new PsLineInfo();
        QueryWrapper<PsLineInfo> wrapper=Wrappers.query();
        wrapper.eq("id",psLineInfo.getId());
        int s=psLineInfoMapper.update(psLineInfo,wrapper);
        boolean fAlg=false;
        if (s>0){
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public List<PsSystemconfig> ChaXia() {
        QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
        wrapper.eq("parentId",10).orderByAsc("id");
        List<PsSystemconfig>  psList =psSysTemConfigMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public List<PsPoleInfo> ChaXiaTiao(String tiAoJiAn) {
        QueryWrapper<PsPoleInfo> wrapper= Wrappers.query();
        wrapper.eq("lineId",tiAoJiAn).select("id","poleCode");
        List<PsPoleInfo> cc=psPoleInfoMapper.selectList(wrapper);
        return cc;
    }


    @Override
    public List<PsPoleInfo> ChaXunMeiXianGan() {
        QueryWrapper<PsPoleInfo> wrapper= Wrappers.query();
            wrapper.isNull("lineName");


        List<PsPoleInfo> aa=psPoleInfoMapper.selectList(wrapper);
        return aa;
    }

    @Override
    public List<Result2> ChaZuiDa(List<PsPoleInfo> aa) {
        List<Result2> s=new ArrayList<>();

        List<Integer> yt=new ArrayList<>();
        if(aa.size()>0){
            for(PsPoleInfo a:aa){
                yt.add(a.getId());
            }
        }
        QueryWrapper<PsPoleInfo> wrapper=Wrappers.query();
        wrapper.in("id",yt).orderByDesc("poleCode").last("limit 1");
        PsPoleInfo o=psPoleInfoMapper.selectOne(wrapper);
        QueryWrapper<PsPoleInfo> wrapper2=Wrappers.query();
        wrapper2.in("id",yt).orderByAsc("poleCode").last("limit 1");
        PsPoleInfo l=psPoleInfoMapper.selectOne(wrapper2);
        Result2 ss=new Result2();
        ss.setMax(o.getPoleCode());
        ss.setMin(l.getPoleCode());
        s.add(ss);
        return s;
    }

    @Override
    public boolean PsDuoPsole(Integer[] aa,String lineCode,String lineName) {

        QueryWrapper<PsPoleInfo> wrapper=Wrappers.query();
        wrapper.in("id",aa);
        System.out.println(aa);
        PsPoleInfo a=new PsPoleInfo();
        a.setLineCode(lineCode);
        a.setLineName(lineName);
        int s=psPoleInfoMapper.update(a,wrapper);
        boolean fAlg=false;
        if (s>0){
            fAlg=true;
        }
        return fAlg;
    }

}
